package cn.ym.mall.service.impl;

import cn.ym.mall.dao.ActivityDao;
import cn.ym.mall.dao.impl.ActivityDaoImpl;
import cn.ym.mall.entity.ActiveJoin;
import cn.ym.mall.entity.ActiveType;
import cn.ym.mall.entity.Activity;
import cn.ym.mall.service.ActivityService;
import cn.ym.mall.utils.DruidUtils;
import cn.ym.mall.vo.Page;

import java.sql.Connection;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 漆家乐
 */
public class ActivityServiceImpl implements ActivityService {
    ActivityDao activityDao = new ActivityDaoImpl();

    /**
     * 发布活动
     *
     * @param activity
     * @return
     * @throws SQLException
     */
    @Override
    public boolean add(Activity activity) throws SQLException {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);
            if (activityDao.add(activity) == 1) {
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 查询所有活动
     *
     * @return
     * @throws SQLException
     */
    @Override
    public List<Activity> getAll() throws SQLException {
        Connection conn = null;
        List<Activity> activityList = new ArrayList<>();
        try {
            conn = DruidUtils.getConnection();
            activityList = activityDao.getAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return activityList;
    }

    /**
     * 分页查询所有活动
     *
     * @param pageNo
     * @param pageSize
     * @return
     * @throws SQLException
     */
    @Override
    public Page<Activity> getActivityPage(Integer pageNo, Integer pageSize) throws SQLException {
        Connection conn = null;
        Page<Activity> page = new Page<Activity>(pageNo,pageSize);
        try {
            conn = DruidUtils.getConnection();
            page.setPageNo(pageNo);
            page.setPageSize(pageSize);
            page.setCount((int)activityDao.getActivityCount());
            page.setList(activityDao.getActivityPage(pageNo,pageSize));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }

        return page;
    }

    /**
     * 查询未结束的活动
     *
     * @param nowTime
     * @return
     * @throws SQLException
     */
    @Override
    public List<Activity> getActiveTypeNotFinished(LocalDateTime nowTime) throws SQLException {
        Connection conn = null;
        List<Activity> activityList = new ArrayList<>();
        try {
            conn = DruidUtils.getConnection();
            activityList = activityDao.getActiveTypeNotFinished(nowTime);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return activityList;
    }

    /**
     * 分页查询未结束的活动
     *
     * @param nowTime
     * @param pageNo
     * @param pageSize
     * @return
     * @throws SQLException
     */
    @Override
    public Page<Activity> getActiveTypeNotFinishedPage(LocalDateTime nowTime, Integer pageNo, Integer pageSize) throws SQLException {
        Connection conn = null;
        Page<Activity> page = new Page<Activity>(pageNo,pageSize);
        try {
            conn = DruidUtils.getConnection();
            page.setPageNo(pageNo);
            page.setPageSize(pageSize);
            page.setCount((int)activityDao.getActivityCount());
            page.setList(activityDao.getActiveTypeNotFinishedPage(nowTime,pageNo,pageSize));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }

        return page;
    }

    /**
     * 查询结束的活动
     *
     * @param nowTime
     * @return
     * @throws SQLException
     */
    @Override
    public List<Activity> getActiveTypeFinished(LocalDateTime nowTime) throws SQLException {
        Connection conn = null;
        List<Activity> activityList = new ArrayList<>();
        try {
            conn = DruidUtils.getConnection();
            activityList = activityDao.getActiveTypeFinished(nowTime);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return activityList;
    }

    /**
     * 分页查询未结束的活动
     *
     * @param nowTime
     * @param pageNo
     * @param pageSize
     * @return
     * @throws SQLException
     */
    @Override
    public Page<Activity> getActiveTypeFinishedPage(LocalDateTime nowTime, Integer pageNo, Integer pageSize) throws SQLException {
        Connection conn = null;
        Page<Activity> page = new Page<Activity>(pageNo,pageSize);
        try {
            conn = DruidUtils.getConnection();
            page.setPageNo(pageNo);
            page.setPageSize(pageSize);
            page.setCount((int)activityDao.getActivityCount());
            page.setList(activityDao.getActiveTypeFinishedPage(nowTime,pageNo,pageSize));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }

        return page;
    }
}
